#include "Tileset.hpp"

namespace maploader
{
	Tileset::Tileset() : myFirstgid(0), mySource(""), myName(""), myWidth(0), myHeight(0), myTileWidth(0), myTileHeight(0), mySpacing(0), myMargin(0), myImageFile(""), myTransparentColor(0), myID(-1) {}

	Tileset Tileset::Load(const pugi::xml_node& node)
	{
		Tileset tileset;
		tileset.myFirstgid = node.attribute("firstgid").as_uint();
		tileset.mySource = std::string(node.child("image").attribute("source").value());

		std::stringstream ss;
		ss << std::hex << node.child("image").attribute("trans").value();
		ss >> tileset.myTransparentColor;

		tileset.myName = std::string(node.attribute("name").value());
		tileset.myTileWidth = node.attribute("tilewidth").as_uint();
		tileset.myTileHeight = node.attribute("tileheight").as_uint();

		tileset.myImageFile = std::string(node.child("image").attribute("source").value());
		tileset.myWidth = node.child("image").attribute("width").as_uint();
		tileset.myHeight = node.child("image").attribute("height").as_uint();
		tileset.myID = node.child("tile").attribute("id").as_uint();

		for (pugi::xml_node prop = node.child("tile").child("properties").child("property"); prop; prop = prop.next_sibling("properties"))
			tileset.myProperties.push_back(Property::Load(prop));

		return tileset;
	}

	unsigned int Tileset::GetFirstGID()
	{
		return myFirstgid;
	}

	std::string Tileset::GetSource()
	{
		return mySource;
	}
}